package com.zhangxiaofanfan.hotcode.first;

/**
 *
 * @date 2024-07-13 03:14:13
 * @author zhangxiaofanfan
 */
public class HotCode088 {
    public static void main(String[] args) {
        HotCode088 hotCode = new HotCode088();
        int[] nums = {2,3,-2,4};
        System.out.println(hotCode.maxProduct(nums));
    }

    public int maxProduct(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        int[] dp = new int[] {1, 1};
        int product = nums[0], temp;
        for (int num : nums) {
            if (num > 0) {
                dp[0] *= num;
                if (dp[1] != 1) {
                    dp[1] *= num;
                }
            } else if (num < 0) {
                temp = dp[0];
                if (dp[1] != 1) {
                    dp[0] = dp[1] * num;
                } else {
                    dp[0] = 1;
                }
                dp[1] = temp * num;
                product = Math.max(product, dp[0]);
            } else {
                product = Math.max(product, 0);
                dp[0] = 1;
                dp[1] = 1;
            }
            product = Math.max(product, dp[0]);
        }
        return product;
    }
}